package com.GUI.Snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//游戏面板，正常游戏都应该在面板上
    //implements KeyListener键盘监听类
public class GamePanel extends JPanel implements KeyListener, ActionListener {

    //蛇
    int lenght;
    int[] snakeX = new int[600];
    int[] snakeY = new int[500];
    //定义蛇头的方向,初始向右
    String direction;
    //小蛇移速（游戏难度）
    String difficulty = "simple";
    int d = 100;
    //游戏当前的状态，开始，结束
    boolean isStart = false;
    //游戏失败
    boolean isFail = false;
    //食物的坐标
    int foodX;
    int foodY;
    //随机数
    Random random =  new Random();
    //计时器
    Timer timer = new Timer(this.d,this);//100ms执行一次
    //分数
    int score;
    int addscore = 10;
    //初始化方法
    public void init(){
        //默认长度
        lenght =3;
        score = 0;
        snakeX[0] = 100;snakeY[0]=100;//脑袋的坐标
        snakeX[1] = 75;snakeY[1]=100;//第一节的坐标
        snakeX[2] = 50;snakeY[2]=100;//第二节的坐标
        //默认方向
        direction = "R";
        //定时器
        timer.start();
        //食物
        foodX = random.nextInt(34)*25 + 25;
        foodY = random.nextInt(24)*25 + 75;
    }
    public GamePanel(){
        //构造器
        init();
        //获得焦点和键盘事件
        this.setFocusable(true);//获得焦点
        this.addKeyListener(this);//获得键盘监听事件
    }


    //绘制面板,游戏所有的面板都使用这个画笔
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        //绘制静态的面板
        this.setBackground(Color.white);
        //绘制头部广告栏
        Data.header.paintIcon(this,g,25,11);
        //画出黑色矩形当游戏空间
        g.fillRect(25,75,850,600);

        //把小蛇画上去,判断蛇头
        switch (direction){
            case "R":
                Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
                break;
            case "L":
                Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
                break;
            case "U":
                Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
                break;
            case "D":
                Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
                break;
        }

        //绘制蛇的身体
        for (int i = 1; i < lenght; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        //绘制食物
        //g.setColor(Color.red);
        //g.fillOval(foodX,foodY,10,10);
        Data.food.paintIcon(this,g,foodX,foodY);

        //绘制分数
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度：" + lenght,750,35);
        g.drawString("分数：" + score,750,55);
        g.drawString("难度：" + "simple",50,60);
        //游戏状态
        if(isStart == false && isFail==false){
            g.setColor(Color.white);
            //设置字体
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",250,300);
            g.drawString("按1、2、3可以调整难度",250,350);
        }

        //失败效果
        if (isFail){
            isStart = false;//撞到立刻停止
            g.setColor(Color.red);
            //设置字体
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏失败,按下空格重新开始",250,300);
        }
    }


    //键盘监听器
    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();//获取按键code
        if(code == KeyEvent.VK_SPACE){
            //空格键
            if(isFail){
                isFail = false;
                //重新开始
                init();
            }else{
                isStart = !isStart;
            }
            repaint();
        }
        //方向
        if (code == KeyEvent.VK_UP) {
            direction = "U";
        }else if (code == KeyEvent.VK_DOWN) {
            direction = "D";
        }else if (code == KeyEvent.VK_LEFT) {
            direction = "L";
        }else if (code == KeyEvent.VK_RIGHT) {
            direction = "R";
        }

        if(code == KeyEvent.VK_1){
            addscore=10;
            difficulty = "simple";
            d = 900;
        } else if(code == KeyEvent.VK_2){
            addscore=20;
            difficulty = "medium";
            d = 600;
        } else if(code == KeyEvent.VK_3){
            addscore=30;
            difficulty = "hard";
            d = 300;
        }
    }

    //事件监听 == 需要固定的刷新频率 1秒10次
    @Override
    public void actionPerformed(ActionEvent e) {
        if(isStart){
            //身体移动
            for (int i = lenght-1; i > 0; i--) {//后一节移到前面
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }
            //判断小蛇移动方向，右移25
            if (direction == "R"){
                snakeX[0] = snakeX[0] +25 ;
            }else if (direction == "U"){
                snakeY[0] = snakeY[0] -25 ;
            }else if (direction == "D"){
                snakeY[0] = snakeY[0] + 25 ;
            }else if (direction == "L"){
                snakeX[0] = snakeX[0] - 25 ;
            }
            //吃食物
            if(snakeX[0]==foodX&&snakeY[0]==foodY){
                //长度加1，并且重新生成食物坐标
                lenght++;
                score = score + addscore;
                foodX = random.nextInt(34)*25 + 25;
                foodY = random.nextInt(24)*25 + 75;
            }
            //边界判断
            if(snakeX[0]>850){
                snakeX[0] = 0;
            }else if(snakeY[0] >750){
                snakeY[0] = 75;
            } else if (snakeX[0] <0) {
                snakeX[0] = 850;
            }else if(snakeY[0] <75){
                snakeY[0] = 750;
            }

            //失败判定
            for (int i = 1; i <lenght ; i++) {
                if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                    isFail =true;
                }
            }
            repaint();//重画
        }
        timer.start();
    }

    @Override
    public void keyReleased(KeyEvent e) {}
    @Override
    public void keyTyped(KeyEvent e) {}

}


